package com.haochang.chunkvod.controller.adapter.order;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.controller.activity.order.OrderChangeActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.order.FoodDetailInfo;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.view.vod.activity.ActivityDialogEnum;
import com.haochang.chunkvod.view.vod.activity.OrderDialogActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

/**
 * 更换商品适配器
 */
public class OrderChangeAdapter extends QuickAdapter
{
	private Context mContext;
	private FoodDetailInfo swapInfo;//被更换的商品信息

	public OrderChangeAdapter(Context context, int layoutResId, List data)
	{
		super(context, layoutResId, data);
		mContext = context;
	}

	public void setChangeData(FoodDetailInfo swapInfo)
	{
		this.swapInfo = swapInfo;
	}

	@Override
	protected void convert(final BaseAdapterHelper helper, final Object object)
	{
		final GoodsInfo item = (GoodsInfo) object;
		helper.setText(R.id.tv_foodName, item.getGoodsName());
		helper.setText(R.id.tv_price, String.format("￥%s/" + item.getUnit(), SaleUtils.getSaleString(item.getPrice())));
		BaseTextView tv_oldPrice = helper.getView(R.id.tv_price_gray);
		if (item.getOriginalPrice() != item.getPrice() && item.getOriginalPrice() > 0)//如果现价不等于门店价 且原价>0
		{
			tv_oldPrice.setVisibility(View.VISIBLE);
			tv_oldPrice.setText(String.format("￥%s/" + item.getUnit(), SaleUtils.getSaleString(item.getOriginalPrice())));
			tv_oldPrice.getPaint().setAntiAlias(true);//抗锯齿
			tv_oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
		} else
		{
			tv_oldPrice.setVisibility(View.GONE);
		}
		helper.getView(R.id.lin_already_order).setVisibility(View.GONE);
		helper.getView(R.id.lin_right).setVisibility(View.GONE);
		helper.getView(R.id.item_layout).setBackgroundColor(mContext.getResources().getColor(R.color.color_f9f9f9));
		helper.getView(R.id.item_layout).setTag(R.id.order_list, item);
		helper.getView(R.id.item_layout).setOnClickListener(onBaseClickListener);
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.item_layout:
				final GoodsInfo item = (GoodsInfo) v.getTag(R.id.order_list);
				if (item.getSurplus() > 0)
				{
					String standard = item.getStandard_types();
					JSONArray array = null;
					try
					{
						if (!TextUtils.isEmpty(standard))
						{
							array = new JSONArray(standard);
						}
					} catch (JSONException e)
					{
						e.printStackTrace();
					}
					if (array != null && array.length() > 0)
					{
						Intent intent = new Intent(mContext, OrderDialogActivity.class);
						intent.putExtra(IntentKey.GOODSSWAP, swapInfo);
						intent.putExtra(IntentKey.GOODSINFO, item);
						intent.putExtra(IntentKey.IS_GOODSSWAP, true);
						intent.putExtra(IntentKey.ACTIVITY_ENUM, ActivityDialogEnum.SIZE);
						intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
						((OrderChangeActivity) mContext).startActivityForResult(intent, IntentCode.FLAVOR_REQUEST);
					} else
					{
						Intent intent = new Intent(mContext, OrderDialogActivity.class);
						intent.putExtra(IntentKey.GOODSSWAP, swapInfo);
						intent.putExtra(IntentKey.GOODSINFO, item);
						intent.putExtra(IntentKey.IS_GOODSSWAP, true);
						intent.putExtra(IntentKey.ACTIVITY_ENUM, ActivityDialogEnum.DEFAULT);
						intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
						((OrderChangeActivity) mContext).startActivityForResult(intent, IntentCode.FLAVOR_REQUEST);
					}
				}
				break;
			default:
				break;
			}
		}
	};
}
